﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class Promotion : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        int z, promotion_round;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }
        }
       
        protected void Button2_Click1(object sender, EventArgs e)
        {
            int i = 0;
            foreach (GridViewRow gr in GridView1.Rows)
            {
                CheckBox CheckBox1 = (CheckBox)gr.FindControl("CheckBox1");
                if (CheckBox1.Checked == true)
                {
                    try
                    {
                        var q = from p in db.Table_Promotions
                                where p.Promotion_id == Int32.Parse(GridView1.Rows[i].Cells[1].Text)
                                select p;
                        q.ToList().ForEach(c => c.Promotion_status = true);
                        db.SubmitChanges();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                i++;
            }
            GridView1.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (GridViewRow gr in GridView1.Rows)
            {
                CheckBox CheckBox1 = (CheckBox)gr.FindControl("CheckBox1");
                if (CheckBox1.Checked == true)
                {
                    try
                    {
                        var q = from p in db.Table_Promotions
                                where p.Promotion_id == Int32.Parse(GridView1.Rows[i].Cells[1].Text)
                                select p;
                        q.ToList().ForEach(c => c.Promotion_status = false);
                        db.SubmitChanges();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                i++;
            }
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                Table_Promotion x = new Table_Promotion();                
                x.Promotion_id = Int32.Parse(TextBox1.Text); 
                x.Promotion_name = TextBox4.Text;
                x.Promotion_discount = Int32.Parse(TextBox2.Text);
                x.Promotion_status = false;
                db.Table_Promotions.InsertOnSubmit(x);
                db.SubmitChanges();

                Table_Promotion_round y = new Table_Promotion_round();
                Select SData = new Select();
                promotion_round = SData.GetId_Promotion_round_id();
                y.Promotion_round_id = promotion_round;
                y.Promotion_id = Int32.Parse(TextBox1.Text); 
                y.Route_id = Int32.Parse(DropDownList1.SelectedValue);                
                db.Table_Promotion_rounds.InsertOnSubmit(y);
                db.SubmitChanges();
            }
            catch (Exception)
            {                
                throw;
            }
            Response.Redirect("Promotion.aspx");
        }
    }
}